<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>课程信息添加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="cno" class="layui-form-label">
                <span class="x-red">*</span>课程编号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="cno" name="cno" required="" lay-verify="check_cno"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>不能重复
            </div>
        </div>

        <div class="layui-form-item">
            <label for="cname" class="layui-form-label">
                <span class="x-red">*</span>课程名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="cname" name="cname" required="" lay-verify="check_cname"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="credit" class="layui-form-label">
                <span class="x-red">*</span>学分
            </label>
            <div class="layui-input-inline">
                <input type="text" id="credit" name="credit" required="" autocomplete="off" class="layui-input"
                       lay-verify="check_credit">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="ctime" class="layui-form-label">
                <span class="x-red">*</span>开课日期
            </label>
            <div class="layui-input-inline">
                <label>
                    <input type="date" min="1960-01-01" max="2099-12-31" name="ctime" class="layui-input"
                           value="" lay-verify="check_ctime" id="ctime">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="quota" class="layui-form-label">
                <span class="x-red">*</span>名额
            </label>
            <div class="layui-input-inline">
                <input type="text" id="quota" name="quota" required="" autocomplete="off" class="layui-input"
                       lay-verify="check_quota">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="add" class="layui-form-label">
            </label>
            <button id="add" class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
        </div>
    </form>
</div>
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script src="./js/x-layui.js" charset="utf-8"></script>
<script>
    let myDate = new Date((new Date).getTime() + 8 * 60 * 60 * 1000);
    let date = myDate.toJSON().split('T').join(' ').substr(0, 10);
    document.getElementById("ctime").value = date;
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form()
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            check_cno: function (value) {
                if (value.length < 1) {
                    return '课程编号不能为空';
                }
                if (value.length > 8) {
                    return '课程编号不能超过8位';
                }
            },
            check_cname: function (value) {
                if (value.length < 1) {
                    return '系部名称不能为空';
                }
            },
            check_credit: function (value) {
                if (value.length < 1) {
                    return '学分不能为空';
                }
            },
            check_ctime: function (value) {
                if (value.length < 1) {
                    return '开课日期不能为空';
                }
            },
            check_quota: function (value) {
                if (value.length < 1) {
                    return '名额不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            layer.confirm('确定添加吗？', {icon: 3, title: '提示添加'}, function (index) {
                $.ajax({
                    url: "/course_add",
                    type: "post",
                    data: data.field,
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data.code === 200) {
                            layer.msg("添加成功!", {icon: 1, time: 1000}, function () {
                                parent.layer.close(index);
                                parent.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000});
                        }
                    }
                });
            });
            return false;
        });
    });
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>

</html>